package m3;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @ClassName Consumer
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/7/5 15:58
 * @Version V1.0
 **/
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumerGroup3");

        //设置name server
        c.setNamesrvAddr("192.168.64.141:9876");
        //指定从哪接收(订阅)消息
        /*
         Tag1
         Tag1||Tag2||Tag3
         *
        */
        c.subscribe("Topic3", "*");
        //设置消息的监听器(回调对象)
        //Concurrently并发的消息监听,会启动多个线程同时并行接收处理消息
        c.setMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(
                    List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    byte[] a = msg.getBody();
                    String s = new String(a);
                    System.out.println("收到: "+s);
                }
//                return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                System.out.println("处理消息失败");
                //稍后要重新消费,回滚,回退消息
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });
        //启动
        c.start();

    }
}
